package com.weavernorth.action.roleChange;

import com.weavernorth.action.roleChange.pojo.roleTable;
import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.ConnectionPool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.action.roleChange
 * @date: 2021/11/18 15:53
 * @description: 【全集团】角色变更：新增或删除数据到角色权限表：hrmrolemembers
 */
public class roleChangeAction implements Action {
    public static ConnectionPool pool=ConnectionPool.getInstance();
    public Connection conn;
    public Statement statement;
    @Override
    public String execute(RequestInfo requestInfo) {
        Logger log = Logger4jUtil.getLoggerByName("/action/roleChangeAction/");
        log.info("角色变更action-begin=======>");
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid=" + strRequestid);
        String strTable = requestInfo.getRequestManager().getBillTableName();
        log.info("表名：" + strTable);
        String strQuery="SELECT a.requestid,b.* FROM "+strTable+" a INNER JOIN "+strTable+"_dt1 b ON a.id=b.mainid WHERE a.requestid='"+strRequestid+"'";
        RecordSet rs=new RecordSet();
        rs.execute(strQuery);
        List<roleTable> list=new ArrayList<>();
        while (rs.next()){
            //操作类型：0-增加，1-删除
            String czlx=Util.null2String(rs.getString("zjyc"));
            //添加类型：1=人力资源;2=分部;3=部门
            String tjlx=Util.null2String(rs.getString("tjlx"));
            //角色id
            String roleid=Util.null2String(rs.getString("js"));
            //本次变更人员
            String ry=Util.null2String(rs.getString("bczjry"));
            //本次变更部门
            String bm=Util.null2String(rs.getString("bcxzbm"));
            //本次变更分部
            String fb=Util.null2String(rs.getString("bcxzfb"));
            //安全级别From
            String seclevelfrom=Util.null2String(rs.getString("seclevelfrom"));
            //安全级别To
            String seclevelto=Util.null2String(rs.getString("seclevelto"));
            //角色安全级别
            String rolelevel=Util.null2String(rs.getString("rolelevel"));
            roleTable tb=new roleTable();
            tb.setCzlx(czlx);
            tb.setTjlx(tjlx);
            tb.setRole(roleid);
            tb.setLevelfrom(seclevelfrom);
            tb.setLevelto(seclevelto);
            tb.setRolelevel(rolelevel);
            if("1".equals(tjlx)){
                //人员
                tb.setIds(ry);
            }else if("2".equals(tjlx)){
                //分部
                tb.setIds(fb);
            }else{
                //部门
                tb.setIds(bm);
            }
            list.add(tb);
        }
        String strMessage="";
        if(list.size()>0){
            log.info("list集合："+list.toString());
            try {
                conn = pool.getConnection();
                statement = conn.createStatement();
                String czlx = "";//操作类型
                String tjlx = "";//添加类型
                String role = "";//角色id
                String from="";//安全级别from
                String to="";//安全级别to
                String rolelevel="";//角色级别
                String ids="";//ids
                String insertSql="";
                String delSql="";
                for (roleTable roleTable : list) {
                    czlx = roleTable.getCzlx();
                    tjlx = roleTable.getTjlx();
                    role = roleTable.getRole();
                    from = roleTable.getLevelfrom();
                    to=roleTable.getLevelto();
                    rolelevel=roleTable.getRolelevel();
                    ids=roleTable.getIds();
                    log.info("ids="+ids);
                    String[] idArry = ids.split(",");
                    //新增操作
                    if ("0".equals(czlx)) {
                        if ("1".equals(tjlx)) {
                            //新增人员
                            for (String s : idArry) {
                                if(!"".equals(s)){
                                    insertSql="insert into hrmrolemembers (resourcetype,roleid,rolelevel,resourceid) values (" +
                                            " '"+tjlx+"','"+role+"','"+rolelevel+"','"+s+"')";
                                    log.info("新增人员sql："+insertSql);
                                    statement.addBatch(insertSql);
                                }
                            }

                        } else {
                            //新增部门/分部
                            for (String s : idArry) {
                                if(!"".equals(s)){
                                    insertSql="insert into hrmrolemembers (resourcetype,roleid,rolelevel,resourceid,seclevelfrom,seclevelto) values (" +
                                            " '"+tjlx+"','"+role+"','"+rolelevel+"','"+s+"','"+from+"','"+to+"')";
                                    log.info("新增部门/分部sql："+insertSql);
                                    statement.addBatch(insertSql);
                                }
                            }
                        }
                    } else {
                        //删除操作
                        for (String s : idArry) {
                            if(!"".equals(s)){
                                delSql="delete hrmrolemembers where resourcetype='"+tjlx+"' and roleid='"+role+"' and resourceid='"+s+"'";
                                log.info("删除sql："+delSql);
                                statement.addBatch(delSql);
                            }
                        }
                    }
                }

                Long begin1 = new Date().getTime();
                statement.executeBatch();
                Long end1 = new Date().getTime();
                log.info("批量更新耗时：" + (end1 - begin1) + "(毫秒)");
                conn.close();
                statement.close();
            }catch (Exception ex){
                strMessage="批处理异常,异常信息："+ex.toString();
                log.info(strMessage);
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;
            }
        }else{
            strMessage="没有可执行的数据";
            log.info(strMessage);
            requestInfo.getRequestManager().setMessagecontent(strMessage);
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
